<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="排班日期" style="width: 308px">
        <el-date-picker
          v-model="daterangeScheduleDate"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="时间段" prop="time">
        <el-select v-model="queryParams.time" placeholder="请选择时间段" clearable>
          <el-option
            v-for="dict in time"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="科室id" prop="departmentId">
        <el-select v-model="queryParams.departmentId" placeholder="请选择科室id" clearable>
          <el-option
            v-for="dict in td_department"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="schedulelistList" @selection-change="handleSelectionChange">
      <el-table-column label="医生姓名" align="center" prop="docterName" />
      <el-table-column label="排班日期" align="center" prop="scheduleDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.scheduleDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="时间段" align="center" prop="time">
        <template #default="scope">
          <dict-tag :options="time" :value="scope.row.time"/>
        </template>
      </el-table-column>
      <el-table-column label="科室" align="center" prop="departmentId">
        <template #default="scope">
          <dict-tag :options="td_department" :value="scope.row.departmentId"/>
        </template>
      </el-table-column>
      <el-table-column label="号源" align="center" prop="count" />
      <el-table-column label="剩余号源" align="center" prop="sycount"/>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改排班信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="schedulelistRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="排班日期" prop="scheduleDate">
          <el-date-picker clearable
            v-model="form.scheduleDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择排班日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="时间段" prop="time">
          <el-select v-model="form.time" placeholder="请选择时间段">
            <el-option
              v-for="dict in time"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="科室" prop="departmentId">
          <el-select v-model="form.departmentId" placeholder="请选择科室id">
            <el-option
              v-for="dict in td_department"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="号源" prop="count">
          <el-input v-model="form.count" placeholder="请输入号源" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Schedulelist">
import { listSchedulelist, getSchedulelist, delSchedulelist, addSchedulelist, updateSchedulelist } from "@/api/schedule/schedulelist";

const { proxy } = getCurrentInstance();
const { time, td_department } = proxy.useDict('time', 'td_department');

const schedulelistList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeScheduleDate = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    docterId: null,
    docterName: null,
    scheduleDate: null,
    time: null,
    departmentId: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询排班信息列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeScheduleDate && '' != daterangeScheduleDate) {
    queryParams.value.params["beginScheduleDate"] = daterangeScheduleDate.value[0];
    queryParams.value.params["endScheduleDate"] = daterangeScheduleDate.value[1];
  }
  listSchedulelist(queryParams.value).then(response => {
    schedulelistList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    docterId: null,
    docterName: null,
    scheduleDate: null,
    time: null,
    createTime: null,
    departmentId: null,
    count: null
  };
  proxy.resetForm("schedulelistRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeScheduleDate.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

getList();
</script>
